package org.chnxi.study.datastructrues.tree;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

public class BinaryTreeDemo {

    public static void main(String[] args) {
        //先手动创建二叉树
        BinaryTree<Person> bt = new BinaryTree<Person>();

        //创建节点
        Person p1 = new Person(1 , "aaa");
        TreeNode<Person> tnp1 = new TreeNode<>();
        tnp1.setE(p1);

        Person p2 = new Person(2 , "bbb");
        TreeNode<Person> tnp2 = new TreeNode<>();
        tnp2.setE(p2);

        Person p3 = new Person(3 , "ccc");
        TreeNode<Person> tnp3 = new TreeNode<>();
        tnp3.setE(p3);

        Person p4 = new Person(4 , "ddd");
        TreeNode<Person> tnp4 = new TreeNode<>();
        tnp4.setE(p4);

        Person p5 = new Person(5 , "eee");
        TreeNode<Person> tnp5 = new TreeNode<>();
        tnp5.setE(p5);

        //手动增加树的关系
        bt.setRoot(tnp1);
        tnp1.setLeft(tnp2);
        tnp1.setRight(tnp3);
        tnp3.setLeft(tnp5);
        tnp3.setRight(tnp4);

        //测试前序遍历
        System.out.println("测试前序遍历：");
        bt.preOrder();
        //测试中序遍历
        System.out.println("测试中序遍历：");
        bt.infixOrder();
        //测试后序遍历
        System.out.println("测试后续遍历：");
        bt.postOrder();

        //测试前序遍历查找
        System.out.println("测试前序遍历查找：");
        TreeNode<Person> sr1 = bt.searchByPreOrder(new Person(5,"eee"));
        System.out.println("前序查找的结果："+sr1);

        //测试前序遍历查找
        System.out.println("测试中序遍历查找：");
        TreeNode<Person> sr2 = bt.searchByInfixOrder(new Person(5,"eee"));
        System.out.println("中序查找的结果："+sr2);

        //测试前序遍历查找
        System.out.println("测试后序遍历查找：");
        TreeNode<Person> sr3 = bt.searchByPostOrder(new Person(5,"eee"));
        System.out.println("后序查找的结果："+sr2);

        //测试节点删除
        System.out.println("测试删除指定节点：");
        System.out.println("删除前树中的数据：");
        bt.preOrder();
        boolean isSucc1 = bt.delTreeNode(new Person(5, "eee"));
        boolean isSucc2 =bt.delTreeNode(new Person(3, "ccc"));
        boolean isSucc3 =bt.delTreeNode(new Person(6, "fff"));
        System.out.println("删除操作结果：oper_1="+isSucc1+";oper_2="+isSucc2+";oper_3="+isSucc3);
        System.out.println("删除后树中的数据：");
        bt.preOrder();
    }
}